. "$PSScriptRoot\fserver.ps1"

function _preview($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            echo "$($item[1]) $($item[3])"
            break;
        } else {
            $index = $index + 1
        }
    }
}

function _copytext($input1) {
    $index = 0
    foreach($item in $server_lists)
    {
        if($item[0] -eq $input1){
            #echo "$item"
            #echo $item[1]
            echo "$($item[4])"
            break;
        } else {
            $index = $index + 1
        }
    }
}

function _ssh(){
	# echo "[Info] this is _ssh()"
    # $select_server =$(_list | fzf)
    $select_server =$(_list | fzf --cycle  --height=81%  --preview-window=bottom:1:wrap
    )
    #echo "[Debug] fzf select server is $select_server "

    # $index = _index($select_server)
    # echo "[Debug] index = $index"
    # $_previe_res = _preview($select_server)
    # echo "[Debug] _previe_res  = $_previe_res"

    echo  "copy $select_server passwd  ..."
    # echo  "$(_copytext "$select_server")" | pbcopy
    Set-Clipboard -Value $(_copytext "$select_server")
    echo  "copy success."
    $cmdString = _preview($select_server)
    echo  "exec $cmdString ..."
    Invoke-Expression $cmdString

}



switch ($args[0])
{
	"" { _ssh }
	preview { _preview($args[1])}
	copytext {_copytext "$args[1]"}
	cmdstr { _cmdstr "$args[1]"}
	default { "Tt is default"}
}